package bb.lanxing.lib.devices.sprint.sync;

import android.util.Pair;
import bb.lanxing.lib.devices.core.sync.AbstractSyncManager;
import bb.lanxing.lib.devices.core.sync.DeviceFile;
import bb.lanxing.lib.devices.core.sync.DeviceFileStatus;
import bb.lanxing.lib.devices.core.sync.FitDeviceFile;
import bb.lanxing.lib.devices.sprint.XossDevice;
import bb.lanxing.lib.devices.sprint.XossDeviceController;
import bb.lanxing.lib.devices.sprint.entity.SprintNav;
import bb.lanxing.lib.devices.sprint.model.SprintNavigationModel;
import bb.lanxing.lib.devices.sprint.utils.DeviceControllerHelper;
import bb.lanxing.lib.devices.sprint.utils.SprintFileHelper;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import rx.Subscriber;

public abstract class AbstractSprintNavigationSyncManager extends AbstractSyncManager {
    private String address;
    private SprintNavigationModel deviceNavigationModel;
    private SprintNavigationModel localModel;

    @Override
    public void abortAll() {
    }

    protected abstract SprintNavigationModel createLocalSprintNavigationModel();

    protected abstract SprintNavigationModel createSprintDeviceNavigationModel(String str);

    @Override
    public void delete(long j) {
    }

    @Override
    public boolean isSynchronised(long j) {
        return false;
    }

    @Override
    public boolean isSynchronisingWithMultiFiles() {
        return false;
    }

    @Override
    public boolean resync(DeviceFile deviceFile) {
        return false;
    }

    @Override
    public boolean sync(String str) {
        return false;
    }

    public AbstractSprintNavigationSyncManager(String str) {
        this.address = str;
        SprintNavigationModel createLocalSprintNavigationModel = createLocalSprintNavigationModel();
        this.localModel = createLocalSprintNavigationModel;
        createLocalSprintNavigationModel.setAddress(str);
        SprintNavigationModel createSprintDeviceNavigationModel = createSprintDeviceNavigationModel(str);
        this.deviceNavigationModel = createSprintDeviceNavigationModel;
        createSprintDeviceNavigationModel.subscribeLushu(new Subscriber<List<SprintNav>>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable th) {
                AbstractSprintNavigationSyncManager.this.localModel.loadNavigation();
            }

            @Override
            public void onNext(List<SprintNav> list) {
                AbstractSprintNavigationSyncManager.this.localModel.loadNavigation();
            }
        });
        this.localModel.subscribeLushu(new Subscriber<List<SprintNav>>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable th) {
                AbstractSprintNavigationSyncManager.this.notifyDeviceFiles(null);
            }

            @Override
            public void onNext(List<SprintNav> list) {
                List lushuToDeviceFiles = AbstractSprintNavigationSyncManager.this.lushuToDeviceFiles(list);
                AbstractSprintNavigationSyncManager.this.setDeviceFiles(lushuToDeviceFiles);
                AbstractSprintNavigationSyncManager.this.notifyDeviceFiles(new ArrayList(lushuToDeviceFiles));
            }
        });
        this.localModel.subscribeNavigationStatus(new Subscriber<Pair<Long, Integer>>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable th) {
            }

            @Override
            public void onNext(Pair<Long, Integer> pair) {
                long longValue = (Long) pair.first;
                int intValue = (Integer) pair.second;
                DeviceFile deviceFileById = AbstractSprintNavigationSyncManager.this.getDeviceFileById(longValue);
                if (deviceFileById != null) {
                    AbstractSprintNavigationSyncManager.this.notifySyncStatus(deviceFileById, intValue);
                }
            }
        });
        this.localModel.subscribeProgressUpdate(new Subscriber<Pair<String, Float>>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable th) {
            }

            @Override
            public void onNext(Pair<String, Float> pair) {
                DeviceFile deviceFileByName;
                if (pair == null || (deviceFileByName = AbstractSprintNavigationSyncManager.this.getDeviceFileByName((String) pair.first)) == null) {
                    return;
                }
                AbstractSprintNavigationSyncManager.this.notifyProgressUpdate(deviceFileByName, (Float) pair.second);
            }
        });
    }

    public List<DeviceFile> lushuToDeviceFiles(List<SprintNav> list) {
        ArrayList arrayList = new ArrayList();
        for (SprintNav sprintNav : list) {
            if (!this.deviceNavigationModel.isExists(sprintNav.getNavServerId())) {
                FitDeviceFile fitDeviceFile = new FitDeviceFile();
                fitDeviceFile.id = sprintNav.getNavServerId();
                fitDeviceFile.name = sprintNav.getNavServerId() + SprintFileHelper.NAV_FILE_STUFFIX;
                arrayList.add(fitDeviceFile);
            }
        }
        return arrayList;
    }

    @Override
    public void readFileList() {
        this.deviceNavigationModel.loadNavigation();
    }

    @Override
    public void abort() {
        SprintNavigationModel sprintNavigationModel = this.localModel;
        if (sprintNavigationModel != null) {
            sprintNavigationModel.abort();
        }
    }

    @Override
    protected boolean onSync(DeviceFile deviceFile) {
        if (deviceFile == null) {
            return false;
        }
        if (deviceFile.getId() <= 0) {
            notifySyncStatus(deviceFile, DeviceFileStatus.STATUS_SYNC_FAIL.getStatus());
            return false;
        }
        String path = deviceFile.getPath();
        File file = path != null ? new File(path) : null;
        if (file == null || !file.exists()) {
            notifySyncStatus(deviceFile, DeviceFileStatus.STATUS_SYNC_FAIL.getStatus());
            return false;
        }
        XossDeviceController xossDeviceController = DeviceControllerHelper.getXossDeviceController(this.address);
        if (!(xossDeviceController instanceof XossDevice)) {
            notifySyncStatus(deviceFile, DeviceFileStatus.STATUS_SYNC_FAIL.getStatus());
            return false;
        }
        this.localModel.upload(path);
        return true;
    }

    @Override
    public void notifySyncStatus(DeviceFile deviceFile, int i, String str, boolean z) {
        super.notifySyncStatus(deviceFile, i, str, z);
        DeviceFileStatus vStatus = DeviceFileStatus.valueOf(i);

        if (vStatus == DeviceFileStatus.STATUS_SYNCED) {
            pop();
        } else if (vStatus == DeviceFileStatus.STATUS_SYNC_FAIL) {
            clearPendingFiles();
        }
    }

    @Override
    public String getPath(long j) {
        DeviceFile deviceFileById = getDeviceFileById(j);
        if (deviceFileById != null) {
            return deviceFileById.getPath();
        }
        return null;
    }

    @Override
    public DeviceFile getDeviceFileByName(String str) {
        if (str == null || !str.endsWith(SprintFileHelper.NAV_FILE_STUFFIX)) {
            return null;
        }
        return super.getDeviceFileByName(str);
    }

    @Override
    public void release() {
        super.release();
        SprintNavigationModel sprintNavigationModel = this.localModel;
        if (sprintNavigationModel != null) {
            sprintNavigationModel.release();
        }
    }
}
